package karileet.meeting.profile.fragment;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import karileet.meeting.profile.ProfileConf;
import karileet.meeting.profile.list.adapter.ListCheckboxItemAdapter;
import karileet.meeting.profile.list.holder.ListCheckboxItemHolder;
import karileet.meeting.profile.list.item.ListCheckboxItem;

import java.util.List;

/**
 * Wrapper class for simple lists (ListCheckboxItem). Class handles initiation of the list and also provides simple
 * on click handler. Population of the list and application state change has to be implemented by subclass
 */
public abstract class AbstractListFragment extends ListFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ListAdapter adapter = new ListCheckboxItemAdapter(getActivity(), populateMainList());
        setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.d(ProfileConf.APP, "Item clicked on position: " + position);
        ListCheckboxItemHolder holder = (ListCheckboxItemHolder) v.getTag();
        ListCheckboxItem item = holder.getTag();
        item.toggle();
        holder.check(!holder.getCb().isChecked());
        onClickAction(holder, position);
    }

    /**
     * Handle application state
     *
     * @param holder   List item holder
     * @param position List item position in list
     */
    abstract protected void onClickAction(ListCheckboxItemHolder holder, int position);

    /**
     * Populate list with items
     *
     * @return List of populated list items
     */
    abstract protected List<ListCheckboxItem> populateMainList();
}
